//
//  NewsMaintainViewController.m
//  CarUser
//
//  Created by 王森林 on 2019/1/18.
//  Copyright © 2019年 王森林. All rights reserved.
//

#import "NewsMaintainViewController.h"
#import "CustomPickerView.h"
#import "UserRes.h"
#import "YQBPlaceTextView.h"
#import "SDCycleScrollView.h"
#import "Car_CarDetailViewController.h"
#import "HomeArticleDetailViewController.h"
@interface NewsMaintainViewController ()<SDCycleScrollViewDelegate,UITextFieldDelegate>
@property (nonatomic, strong) UIScrollView *scroll;
@property  (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIView *centerView;
@property (nonatomic, strong) UIView *centerView1;
@property (nonatomic, strong) UIView *downView;


@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UITextField *contantField;
@property (nonatomic, strong) UITextField *mobileField;
@property (nonatomic, strong) UITextField *cityField;
@property (nonatomic, strong) YQBPlaceTextView *leaveTextView;

@property (nonatomic, strong) NSArray *imgsAry;
@property (strong, nonatomic) SDCycleScrollView *mScrollView;
@end

@implementation NewsMaintainViewController

#define BTNTAG  100000
- (void)viewDidLoad {
    [super viewDidLoad];
     self.title  =  @"维修保养";
       [self fetchDataWithNet];
    // Do any additional setup after loading the view.
}


-  (void)fetchDataWithNet
{
    //获取广告顶部
    NSDictionary *params = @{
                             @"FKEY":[CommonMethod createMD5Key:@"PZLXDM"],
                             @"PZLXDM":@"10"
                             
                             };
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD showWithStatus:nil];
    });
    
    [[ZY_AfnNetManager shareInstance]postWithURL:Car_API_28 params:params md5Paraname:nil success:^(id  _Nullable response) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
        
        NSDictionary *resultDict  = response;
        self.imgsAry  = [resultDict objectForKey:@"list"];
        [self createUI];
        
        
    } fail:^(NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
        });
        
    }];
    
    
}


- (void)createUI
{
    [self.view addSubview:self.scroll];
    self.scroll.frame = CGRectMake(0, 0, BOUNDWIDTH, BOUNDHEIGHT);
    [self.scroll addSubview:self.topView];
    self.topView.frame = CGRectMake(0, 0, BOUNDWIDTH, 360);
    
//    UIImageView *topImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, BOUNDWIDTH, 150)];
//    topImageView.image = IMG(@"car_test5");
//    [self.topView addSubview:topImageView];
    self.mScrollView = ({
        SDCycleScrollView *scrollView = [[SDCycleScrollView alloc] initWithFrame:CGRectZero];
        scrollView.backgroundColor = [UIColor whiteColor];
        scrollView.delegate = self;
        scrollView.imageURLStringsGroup = @[];
        scrollView.pageDotColor = [UIColor whiteColor];
        scrollView.currentPageDotColor = KRGB(255, 255, 255, 0.6);
        [self.topView addSubview:scrollView];
        scrollView;
    });
    self.mScrollView.frame = CGRectMake(0, 0, BOUNDWIDTH, 150);
   
    
    NSMutableArray *imgsDataAry = [[NSMutableArray alloc] init];
    [self.imgsAry enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSDictionary *dict = [self.imgsAry objectAtIndex:idx];
        [imgsDataAry addObject:APPEND_URL(BASE_URL, [dict objectForKey:@"IMGURL"])];
    }];
    
    self.mScrollView.imageURLStringsGroup  =  imgsDataAry;
    
    
    
    NSArray *ary = @[@{@"imageName":@"function1",@"title":@"小保养"},
                         @{@"imageName":@"function2",@"title":@"大保养"},
                         @{@"imageName":@"function3",@"title":@"节气门清洗"},
                         @{@"imageName":@"function4",@"title":@"更换火花塞"},
                         @{@"imageName":@"function5",@"title":@"轮廓保养"},
                         @{@"imageName":@"function6",@"title":@"更换刹车灯"},
                         @{@"imageName":@"function7",@"title":@"更换蓄电池"},
                         @{@"imageName":@"function8",@"title":@"发动机仓保养"}
                         ];
    
    
    [ary enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *imageName  =   [obj  objectForKey:@"imageName"];
        NSString *title = [obj objectForKey:@"title"];
        [self createBtnWithImg:imageName withTitle:title withIndex:idx];
    }];
    


//    [self.scroll addSubview:self.centerView];
//    self.centerView.frame  = CGRectMake(0, 370, BOUNDWIDTH, 120);
//    [self createCenterView];
    
    [self.scroll addSubview:self.centerView1];
    self.centerView1.frame  = CGRectMake(0, 370, BOUNDWIDTH, 225);
    [self createCenterView1];

    [self.scroll addSubview:self.downView];
    self.downView.frame = CGRectMake(0, 605, BOUNDWIDTH, 450);
    [self  createDownView];
    
    self.scroll.contentSize = CGSizeMake(BOUNDWIDTH, 1200);
    
}



/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    NSDictionary *dict = [self.imgsAry objectAtIndex:index];
    NSLog(@"dict%@",dict);
    NSString *type = [dict objectForKey:@"APPTYPE"];
    if (type.integerValue == 1) {
        //文章详情
    }
    
    if (type.integerValue == 2) {
        //车辆详情
        Car_CarDetailViewController *push = [[Car_CarDetailViewController alloc]init];
        push.SERIES_ID = [dict objectForKey:@"APPURL"];
        [self.navigationController pushViewController:push animated:YES];
        
    }
}

/** 图片滚动回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index {
    
}

//提交
- (void)handleSumbitAction:(id)sender
{
    
    //保存预约

    if ([CommonMethod isBlankString:self.dateLabel.text]) {
        [SVProgressHUD showErrorWithStatus:@"请选择预约日期"];
        return ;
    }


    if ([CommonMethod isBlankString:self.mobileField.text]) {
        [SVProgressHUD showErrorWithStatus:@"请输入联系电话"];
        return ;
    }

    if ([CommonMethod isBlankString:self.contantField.text]) {
        [SVProgressHUD showErrorWithStatus:@"请输入联系人"];
        return ;
    }
    
//    if (self.mobileField.text.length > 11) {
//        [SVProgressHUD showErrorWithStatus:@""];
//        return ;
//    }
    
//    if ([CommonMethod isBlankString:self.cityField.text]) {
//        [SVProgressHUD showErrorWithStatus:@"请输入城市"];
//        return ;
//    }
//    if ([CommonMethod isBlankString:self.leaveTextView.text]) {
//        [SVProgressHUD showErrorWithStatus:@"请输入留言"];
//        return ;
//    }



    UserRes *user = [CommonMethod getUser];
    if (!user) {
        [[NSNotificationCenter defaultCenter]postNotificationName:NotificationCenterName_AlertLoginVC object:nil];
        return;
    }
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithDictionary:@{
                                                                                   @"FKEY":[CommonMethod createMD5Key:@"TOKEN"],
                                                                                   @"TOKEN":user.TOKEN,
                                                                                   @"USERNAME":user.mobile,
                                                                                   @"LXR000":self.contantField.text,
                                                                                   @"LXDH00":self.mobileField.text,
                                                                                   @"YYRQ00":self.dateLabel.text,
                                                                                   
                                                                                   }];
    if (![CommonMethod isBlankString:self.cityField.text]) {
        [params setObject:self.cityField.text forKey:@"SZCS00"];
    }
    if ([CommonMethod isBlankString:self.leaveTextView.text]) {
        [params setObject:self.leaveTextView.text forKey:@"XMMS00"];
    }
//
//    NSDictionary *params = @{
//                             @"FKEY":[CommonMethod createMD5Key:@"TOKEN"],
//                             @"TOKEN":user.TOKEN,
//                             @"USERNAME":user.mobile,
//                             @"LXR000":self.contantField.text,
//                             @"LXDH00":self.mobileField.text,
//                             @"YYRQ00":self.dateLabel.text,
//                             @"SZCS00":self.cityField.text,
//                             @"XMMS00":self.leaveTextView.text
//
//                             };

    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD showWithStatus:nil];
    });

    [[ZY_AfnNetManager shareInstance]postWithURL:Car_API_51 params:params md5Paraname:nil success:^(id  _Nullable response) {

        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });

        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showInfoWithStatus:@"预约成功"];
            //                [SVProgressHUD dismissWithDelay:10];
        });
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        });




        //        NSDictionary *resultDict  = response;
        //        self.imgsAry  = [resultDict objectForKey:@"list"];
        //        NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
        //        [self.mainTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];


    } fail:^(NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
        });

    }];
//
}

//预约时间点击
- (void)handleDateTapAction:(UIGestureRecognizer *)gesture
{
    //    NSArray *ary = [self createTimeChoose];
    [CustomPickerView initWithFrame:CGRectZero withModeType:DatePicker andTitleLab:@"选择时间" componentAry:nil andHasChoose:nil andCallBack:^(id resutData) {
        NSLog(@"%@",resutData);
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"YYYY-MM-dd"];
        NSString *currentTimeString = [formatter stringFromDate:resutData];
        self.dateLabel.text = currentTimeString;
        
        
        
    }];
    
    
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //        if ([_NameTextField isFirstResponder]) {
    //                [_IDCardTextField becomeFirstResponder];
    //            } else if([_IDCardTextField isFirstResponder]) {
    //                    [_MobileTextField becomeFirstResponder];
    //                }
    
    if ([self.contantField isFirstResponder]) {
        [self.mobileField becomeFirstResponder];
    } else if ([self.mobileField isFirstResponder]) {
        [self.cityField becomeFirstResponder];
        
    } else if ([self.cityField isFirstResponder]) {
        [self.leaveTextView becomeFirstResponder];
    }
    
        return YES;
}

- (void)textFieldDidChange:(UITextField *)textField
{
    if (textField == self.mobileField) {
        if (textField.text.length > 11) {
      　UITextRange *markedRange = [textField markedTextRange];
        　if (markedRange) {
            return;
                         }
            //Emoji占2个字符，如果是超出了半个Emoji，用15位置来截取会出现Emoji截为2半
            //超出最大长度的那个字符序列(Emoji算一个字符序列)的range
            NSRange range = [textField.text rangeOfComposedCharacterSequenceAtIndex:11];
            textField.text = [textField.text substringToIndex:range.location];
        }
    }
}


- (void)createDownView
{
    //预约日期
    UIView *dateView =  [[UIView alloc] initWithFrame:CGRectMake(0, 20,BOUNDWIDTH , 40)];
    UITapGestureRecognizer *dateTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDateTapAction:)];
    [dateView addGestureRecognizer:dateTap];
    [self.downView addSubview:dateView];
    
    NSMutableAttributedString *datestr = [[NSMutableAttributedString alloc] initWithString:@"预约日期*"];
    [datestr addAttribute:NSForegroundColorAttributeName value:RGB(51, 51, 51) range:NSMakeRange(0,4)];
    [datestr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(4,1)];
    UILabel *dateText  = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 80, 20)];
    dateText.attributedText = datestr;
//    dateText.text  = @"预约日期" ;
//    dateText.textColor = Common_TextLowColor;
    dateText.font = FONT_SIZE(14);
    [dateView addSubview:dateText];
    
    
    
    self.dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(110, 10, BOUNDWIDTH - 150, 20)];
    self.dateLabel.textColor  = Common_TextColor;
    self.dateLabel.textAlignment = NSTextAlignmentRight;
    self.dateLabel.font  = FONT_SIZE(14);
    [dateView addSubview: self.dateLabel];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    NSDate *datenow = [NSDate date];
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    self.dateLabel.text = currentTimeString;
    
    
    UIImageView *arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(BOUNDWIDTH - 23, 13, 8, 14)];
    arrowImageView.image = IMG(@"home_arrow");
    [dateView addSubview:arrowImageView];
    
    
   
    
    //联系人
    UIView *contantView  = [[UIView alloc] initWithFrame:CGRectMake(0, 70, BOUNDWIDTH, 40)];
    [self.downView addSubview:contantView];
    
    
    NSMutableAttributedString *contantstr = [[NSMutableAttributedString alloc] initWithString:@"联系人*"];
    [contantstr addAttribute:NSForegroundColorAttributeName value:RGB(51, 51, 51) range:NSMakeRange(0,3)];
    [contantstr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(3,1)];
    UILabel *contantText  = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 80, 20)];
    contantText.attributedText =  contantstr;
//    contantText.text  = @"联系人" ;
//    contantText.textColor = Common_TextLowColor;
    contantText.font = FONT_SIZE(14);
    [contantView addSubview:contantText];
    
    
    self.contantField  = [[UITextField alloc] initWithFrame:CGRectMake(115, 0, BOUNDWIDTH - 125, 40)];
    self.contantField.layer.cornerRadius  = 15;
    self.contantField.layer.masksToBounds = YES;
    self.contantField.backgroundColor =  self.view.backgroundColor;
    self.contantField.textAlignment = NSTextAlignmentLeft;
    [self.contantField setValue:[NSNumber numberWithInt:10] forKey:@"paddingLeft"];
    self.contantField.returnKeyType = UIReturnKeyNext;
    self.contantField.delegate = self;
    self.contantField.font = FONT_SIZE(14);
    [contantView addSubview:self.contantField];
    
    
    //联系电话
    UIView *mobileView  = [[UIView alloc] initWithFrame:CGRectMake(0, 120, BOUNDWIDTH, 40)];
    [self.downView addSubview:mobileView];
    
    
    NSMutableAttributedString *mobilestr = [[NSMutableAttributedString alloc] initWithString:@"联系电话*"];
    [mobilestr addAttribute:NSForegroundColorAttributeName value:RGB(51, 51, 51) range:NSMakeRange(0,4)];
    [mobilestr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(4,1)];
    UILabel *mobileText  = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 80, 20)];
    mobileText.attributedText = mobilestr;
//    mobileText.text  = @"联系电话" ;
//    mobileText.textColor = Common_TextLowColor;
    mobileText.font = FONT_SIZE(14);
    [mobileView addSubview:mobileText];
    
    
    self.mobileField  = [[UITextField alloc] initWithFrame:CGRectMake(115, 0, BOUNDWIDTH - 125, 40)];
    self.mobileField.layer.cornerRadius  = 15;
    self.mobileField.layer.masksToBounds = YES;
    self.mobileField.backgroundColor =  self.view.backgroundColor;
    self.mobileField.textAlignment = NSTextAlignmentLeft;
     [self.mobileField setValue:[NSNumber numberWithInt:10] forKey:@"paddingLeft"];
    self.mobileField.returnKeyType = UIReturnKeyNext;
    self.mobileField.keyboardType = UIKeyboardTypeNumberPad;
    self.mobileField.delegate = self;
    [self.mobileField addTarget:self action:@selector(textFieldDidChange:)
               forControlEvents:UIControlEventEditingChanged];
    self.mobileField.font = FONT_SIZE(14);
    [mobileView addSubview:self.mobileField];
    
    
    
    //所在城市"
    UIView *cityView  = [[UIView alloc] initWithFrame:CGRectMake(0, 170, BOUNDWIDTH, 40)];
    [self.downView addSubview:cityView];
    
    
    UILabel *cityText  = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 80, 20)];
    cityText.text  = @"所在城市" ;
    cityText.textColor = RGB(51, 51, 51);
    cityText.font = FONT_SIZE(14);
    [cityView addSubview:cityText];
    
    
    self.cityField  = [[UITextField alloc] initWithFrame:CGRectMake(115, 0, BOUNDWIDTH - 125, 40)];
    self.cityField.layer.cornerRadius  = 15;
    self.cityField.layer.masksToBounds = YES;
    self.cityField.backgroundColor =  self.view.backgroundColor;
    self.cityField.textAlignment = NSTextAlignmentLeft;
    [self.cityField setValue:[NSNumber numberWithInt:10] forKey:@"paddingLeft"];
    self.cityField.returnKeyType = UIReturnKeyNext;
    self.cityField.delegate = self;
      self.cityField.font = FONT_SIZE(14);
    [cityView addSubview: self.cityField];
    
    // 留言
    UIView *leaveView  = [[UIView alloc] initWithFrame:CGRectMake(0, 220, BOUNDWIDTH, 80)];
    [self.downView addSubview:leaveView];
    
    
    UILabel *leaveText  = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 80, 60)];
    leaveText.text  = @"留言" ;
    leaveText.textColor = RGB(51, 51, 51);
    leaveText.font = FONT_SIZE(14);
    [leaveView addSubview:leaveText];
    
    
    
    self.leaveTextView = [[YQBPlaceTextView alloc]init];
    self.leaveTextView.frame = CGRectMake(115, 0, BOUNDWIDTH-125, 80);
    self.leaveTextView.font = FONT_SIZE(14);
    self.leaveTextView.textColor = Common_TextColor;
    self.leaveTextView.layer.cornerRadius  = 15;
    self.leaveTextView.layer.masksToBounds = YES;
    self.leaveTextView.backgroundColor =  self.view.backgroundColor;
    self.leaveTextView.textAlignment = NSTextAlignmentLeft;
    self.leaveTextView.returnKeyType = UIReturnKeyNext;
//    [self.leaveTextView setValue:[NSNumber numberWithInt:10] forKey:@"paddingLeft"];
//    self.leaveTextView.delegate = self;
    self.leaveTextView.textContainerInset = UIEdgeInsetsMake(5,5, 5, 5);
    [leaveView addSubview:self.leaveTextView];
//    self.cardnoField  = [[UITextField alloc] initWithFrame:CGRectMake(110, 0, BOUNDWIDTH - 125, 40)];
//    self.cardnoField.layer.cornerRadius  = 15;
//    self.cardnoField.layer.masksToBounds = YES;
//    self.cardnoField.backgroundColor =  self.view.backgroundColor;
//    self.cardnoField.textAlignment = NSTextAlignmentRight;
//    [cardnoView addSubview:self.cardnoField];
    
    
    
    UIButton  *sumbitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sumbitBtn.frame = CGRectMake(15, 330, BOUNDWIDTH - 30, 45);
    [sumbitBtn setTitle:@"提交" forState:UIControlStateNormal];
    sumbitBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    sumbitBtn.backgroundColor = RGB(254, 218, 60);
    sumbitBtn.layer.cornerRadius = 22;
    sumbitBtn.layer.masksToBounds = YES;
    [sumbitBtn setTitleColor:Common_TextColor forState:UIControlStateNormal];
    [sumbitBtn addTarget:self action:@selector(handleSumbitAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.downView addSubview:sumbitBtn];
    
    
    
}

- (void)createCenterView1
{
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, BOUNDWIDTH, 18)];
    titleLabel.text = @"合作伙伴";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = FONT_SIZE(17);
    titleLabel.textColor = Common_TextColor;
    [self.centerView1 addSubview:titleLabel];
    
    NSArray *ary = @[@"http://wuhanlinkonggang.com/car/images/maintenance/partner01.jpg",
                     @"http://wuhanlinkonggang.com/car/images/maintenance/partner02.jpg",
                     @"http://wuhanlinkonggang.com/car/images/maintenance/partner03.jpg",
                     @"http://wuhanlinkonggang.com/car/images/maintenance/partner04.jpg",
                     @"http://wuhanlinkonggang.com/car/images/maintenance/partner05.jpg",
                     @"http://wuhanlinkonggang.com/car/images/maintenance/partner06.jpg",
                     @"http://wuhanlinkonggang.com/car/images/maintenance/partner07.jpg",
                     @"http://wuhanlinkonggang.com/car/images/maintenance/partner08.jpg",
                     @"http://wuhanlinkonggang.com/car/images/maintenance/partner09.jpg",
                     @"http://wuhanlinkonggang.com/car/images/maintenance/partner10.jpg",
                     @"http://wuhanlinkonggang.com/car/images/maintenance/partner11.jpg"
                ];
    [ary enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        
        float width = (BOUNDWIDTH - 30-30)/4;
        float  height  = 50;
        
        
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.frame =  CGRectMake(15+(width+10)*(idx%4), 35+ (10+height)*(idx/4), width, height);
//        imageView.image = IMG(obj);
        [imageView sd_setImageWithURL:[NSURL URLWithString:obj]];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.clipsToBounds = YES;
        [self.centerView1 addSubview:imageView];
        
//        UIButton *btn2  = [UIButton buttonWithType:UIButtonTypeCustom];
//        btn2.frame = CGRectMake(15+(width+10)*(idx%4), 35+ (10+height)*(idx/4), width, height);
//        [btn2 setBackgroundImage:IMG(obj) forState:UIControlStateNormal];
//        [self.centerView1 addSubview:btn2];
        
    }];
    
    
}

- (void)createCenterView
{
    
    float width = (BOUNDWIDTH - 30 - 20)/3;
    UIButton *btn1  = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame = CGRectMake(15, 10, width, 100);
    [btn1 setBackgroundImage:IMG(@"car_test4") forState:UIControlStateNormal];
    [self.centerView addSubview:btn1];
    
    UIButton *btn2  = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame = CGRectMake(15+10+width, 10, width+5, 50);
    [btn2 setBackgroundImage:IMG(@"car_test4") forState:UIControlStateNormal];
    [self.centerView addSubview:btn2];
    
   
    UIButton *btn3  = [UIButton buttonWithType:UIButtonTypeCustom];
    btn3.frame = CGRectMake(15+width*2+10, 10, width+10, 50);
    [btn3 setBackgroundImage:IMG(@"car_test4") forState:UIControlStateNormal];
    [self.centerView addSubview:btn3];

    UIButton *btn4  = [UIButton buttonWithType:UIButtonTypeCustom];
    btn4.frame = CGRectMake(15+10+width, 60, width*2+10, 50);
    [btn4 setBackgroundImage:IMG(@"car_test4") forState:UIControlStateNormal];
    [self.centerView addSubview:btn4];
    
    
}

- (void)handleBtnAction:(id)sender
{
    UIButton *btn = sender;
    NSInteger tag = btn.tag - BTNTAG;
    switch (tag) {
        case 0:
        {
            HomeArticleDetailViewController *controller  = [[HomeArticleDetailViewController alloc] init];
            controller.article_id = @"b87971b9caf14c719b0bdb6c8403f121";
            controller.title = @"维修保养服务-小保养";
            [self.navigationController pushViewController:controller animated:YES];
            
        }
            break;
        case 1:
        {
            HomeArticleDetailViewController *controller  = [[HomeArticleDetailViewController alloc] init];
            controller.article_id = @"456d3c0f3ea8499db7ec494210a76239";
            controller.title = @"维修保养服务-大保养";
            [self.navigationController pushViewController:controller animated:YES];
            
        }
            break;
        case 2:
        {
            HomeArticleDetailViewController *controller  = [[HomeArticleDetailViewController alloc] init];
            controller.article_id = @"315c3797d8684c3c803b6dbbee0c2b64";
            controller.title = @"维修保养服务-节气门清洗";
            [self.navigationController pushViewController:controller animated:YES];
            
        }
            break;
        case 3:
        {
            HomeArticleDetailViewController *controller  = [[HomeArticleDetailViewController alloc] init];
            controller.article_id = @"0ecc37bfa6ae4fa5964c431198befa24";
            controller.title = @"维修保养服务-更换火花塞";
            [self.navigationController pushViewController:controller animated:YES];
            
            
        }
            break;
        case 4:
        {
                        HomeArticleDetailViewController *controller  = [[HomeArticleDetailViewController alloc] init];
                        controller.article_id = @"e18de022e7aa44eba6ef76157a25c2d9";
                        controller.title = @"维修保养服务-轮廓保养";
                        [self.navigationController pushViewController:controller animated:YES];
            
        }
            break;
        case 5:
        {
            HomeArticleDetailViewController *controller  = [[HomeArticleDetailViewController alloc] init];
            controller.article_id = @"b6658044e38043d39821c342086afbd9";
            controller.title = @"维修保养服务-更换刹车油";
            [self.navigationController pushViewController:controller animated:YES];
            
        }
            break;
        case 6:
        {
            
            HomeArticleDetailViewController *controller  = [[HomeArticleDetailViewController alloc] init];
            controller.article_id = @"605402dfe9b8403aac885b3e002ebf52";
            controller.title = @"维修保养服务-更换蓄电池";
            [self.navigationController pushViewController:controller animated:YES];
        }
            break;
        case 7:
        {
            HomeArticleDetailViewController *controller  = [[HomeArticleDetailViewController alloc] init];
            controller.article_id = @"4693c37713bb4b4096c3078a1849b2a9";
            controller.title = @"维修保养服务-发动机舱养护";
            [self.navigationController pushViewController:controller animated:YES];
            
        }
            break;
            
        default:
            break;
    }

    
}

-  (void )createBtnWithImg:(NSString *)imgName withTitle:(NSString *)title withIndex:(NSInteger)index
{
    UIImage *image = IMG(imgName);
    //    float marX = 40;
    //    float  width  = (BOUNDWIDTH  - IMGWIDTH(image)*4-marX*2)/3;
    float width = BOUNDWIDTH/4;
    float  height  = 90;
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame =  CGRectMake(width*(index%4), 165+ height*(index/4), width, height);
    btn.tag = BTNTAG+index;
    [btn addTarget:self action:@selector(handleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    //    [btn setTitle:title forState:UIControlStateNormal];
    //    [btn setImage:image forState:UIControlStateNormal];
    //    [btn setTitleColor:Common_TextColor forState:UIControlStateNormal];
    //    btn.titleLabel.font = FONT_SIZE(12);
    //    btn.titleLabel.textAlignment  =  NSTextAlignmentCenter;
    //    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示
    //    //    UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)
    //    [btn setTitleEdgeInsets:UIEdgeInsetsMake(btn.imageView.frame.size.height-20 ,-btn.imageView.frame.size.width, 0.0,0.0)];//文字距离上边框的距离增加imageView的高度，距离左边框减少imageView的宽度，距离下边框和右边框距离不变
    //    [btn setImageEdgeInsets:UIEdgeInsetsMake( -btn.titleLabel.bounds.size.height-40-8, 0.0,0.0, -btn.titleLabel.bounds.size.width)];//图片距离右边框距离减少图片的宽度，其它不边
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((width-50)/2, 0, 50, 50)];
    imageView.image = image;
    [btn addSubview:imageView];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 60, width, 15)];
    titleLabel.text = title;
    titleLabel.font = FONT_SIZE(12);
    titleLabel.textAlignment  =  NSTextAlignmentCenter;
    titleLabel.textColor = Common_TextColor;
    [btn addSubview:titleLabel];
    
    
    
    [self.topView addSubview:btn];
}

-  (UIScrollView *)scroll
{
    if (!_scroll) {
        _scroll = [UIScrollView new];
    }
    return _scroll;
    
}

-  (UIView *)topView
{
    if (!_topView) {
        _topView = [UIView new];
        _topView.backgroundColor = [UIColor whiteColor];
    }
    return _topView;
    
}

-  (UIView *)centerView
{
    if (!_centerView) {
        _centerView = [UIView new];
        _centerView.backgroundColor = [UIColor whiteColor];
    }
    return _centerView;
    
}

-  (UIView *)centerView1
{
    if (!_centerView1) {
        _centerView1 = [UIView new];
        _centerView1.backgroundColor = [UIColor whiteColor];
    }
    return _centerView1;
    
}

-  (UIView *)downView
{
    if (!_downView) {
        _downView = [UIView new];
        _downView.backgroundColor = [UIColor whiteColor];
    }
    return _downView;
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
